package com.aj.activity.fragment;

import java.io.Serializable;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.aj.activity.R;
import com.aj.activity.adapter.ChildPhotoAdapter;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.BabyPhotoInfo;
import com.aj.databases.entry.FamilySpace;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.BabyPhotoRequest;
import com.aj.http.response.BabyPhotoResponse;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.utils.Contants;

public class ChildPhotoFragment extends Fragment {
	private ListView listView;
	private FamilySpace familySpace;
	private ChildPhotoAdapter childPhotoAdapter;
	private List<BabyPhotoInfo> babyPhotoInfoArrayList;

	public ChildPhotoFragment() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_childphoto, container,
				false);
		initView(view);
		return view;
	}

	private void initView(View view) {
		listView = (ListView) view.findViewById(R.id.list);
		familySpace = (FamilySpace) getActivity().getIntent()
				.getSerializableExtra(Contants.USERINFOFLAG_STRING);
		resume();
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (babyPhotoInfoArrayList.get(position)
						.getAlbumPhotoInfoList() == null
						|| babyPhotoInfoArrayList.get(position)
								.getAlbumPhotoInfoList().size() == 0) {
					return;
				}
				Intent intent = new Intent(Contants.PHOTO_INFO_ACTION);
				intent.putExtra("albumList",
						(Serializable) babyPhotoInfoArrayList.get(position)
								.getAlbumPhotoInfoList());
				startActivity(intent);
			}
		});
	}

	public void resume() {
		UserDao userDao = new UserDao(getActivity());
		new BabyPhotoRequest().excute(getActivity(),
				userDao.getCurrentUser(LoveHomeApplication.token).getUser_id(),
				familySpace.getId(), new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							BabyPhotoResponse response = new BabyPhotoResponse(
									new JSONObject(result));
							Common common = response.getCommon();
							if (common != null
									&& common.getReturnCode() != null
									&& common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								babyPhotoInfoArrayList = response
										.getBabyPhotoArrayInfo()
										.getPrayLetterInfoList();
								if (babyPhotoInfoArrayList != null
										&& babyPhotoInfoArrayList.size() > 0) {
									childPhotoAdapter = new ChildPhotoAdapter(
											getActivity(),
											babyPhotoInfoArrayList);
									listView.setAdapter(childPhotoAdapter);
								}
							}
							Toast.makeText(getActivity(), common.getErrorMsg(),
									Toast.LENGTH_LONG).show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(getActivity(), common.getErrorMsg(),
									Toast.LENGTH_LONG).show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

}
